package com.hakim.wfb.customer.service.impl;

import com.hakim.wfb.customer.params.trans.*;
import com.hakim.wfb.customer.result.trans.PointExchangeCalculateView;
import com.hakim.wfb.customer.service.TransactionService;
import com.hakim.wfb.customer.service.biz.TransactionBizService;
import com.hakim.wfb.customer.service.biz.TransPointBizService;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.common.utils.NumberUtils;
import com.hkfs.fundamental.lock.redis.LockService;
import com.hakim.wfb.common.util.LockerUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by Administrator on 2017/7/1.
 */
@Service("transactionService")
public class TransactionServiceImpl implements TransactionService {

    @Autowired
    private TransactionBizService transBiz;

    @Autowired
    private TransPointBizService transPointBizService;

    @Autowired
    private LockService lockService;

    @Override
    public ResultEx placeOrder(PlaceOrderParameter param) {

        ResultEx resultEx = new ResultEx();

        if (StringUtils.isEmpty(param.getUserId())) {
            return resultEx.makeFailedResult();
        }

        Integer lockId = NumberUtils.random(0, 1000000);
        Boolean isLockSuccess = false;
        try {
            isLockSuccess = lockService.lock(LockerUtils.UNIQUE_LOCK_Key, LockService.DEFAULT_TRY_LOCK_MILLIS,
                    LockService.DEFAULT_AUTO_UNLOCK_MILLIS, lockId);
            if (!isLockSuccess) {
                return resultEx.makeFailedResult("锁定出错！");
            }
            resultEx = transBiz.placeOrder(param);
        } catch (Exception e) {
            resultEx = new ResultEx();
            return resultEx.makeFailedResult();
        } finally {
            if (isLockSuccess) {
                lockService.unlock(LockerUtils.UNIQUE_LOCK_Key, lockId);
            }
        }
        return resultEx;
    }

    @Override
    public ResultEx createTransPointOrder(GetTransPointParameter param) {
        ResultEx resultEx = new ResultEx();

        if (param.getSessionIdentity() == null || param.getSessionIdentity().getUuid() == null || param.getSessionIdentity().getUuid().equals(""))
            return resultEx.makeFailedResult();

        Integer lockId = NumberUtils.random(0, 1000000);
        Boolean isLockSuccess = false;
        try {
            isLockSuccess = lockService.lock(LockerUtils.UNIQUE_LOCK_Key, LockService.DEFAULT_TRY_LOCK_MILLIS,
                    LockService.DEFAULT_AUTO_UNLOCK_MILLIS, lockId);
            if (!isLockSuccess) {
                return resultEx.makeFailedResult("锁定出错！");
            }
            resultEx = transPointBizService.createTransOrder(param);
        } catch (Exception e) {
            resultEx = new ResultEx();
            return resultEx.makeFailedResult();
        } finally {
            if (isLockSuccess) {
                lockService.unlock(LockerUtils.UNIQUE_LOCK_Key, lockId);
            }
        }
        return resultEx;
    }

/*
    @Override
    public ListResultEx<PointRecordView> getPointRecords(GetPointRecordParameter param){
        ListResultEx<PointRecordView> result = new ListResultEx<PointRecordView>();
        List<PointRecordView> listView  = new ArrayList<>();
        List<PointRecord> list = pointRecordBiz.getPointRecords(param);

        //json重整
        for (int i = 0; i < list.size(); i++) {
            PointRecordView order = new PointRecordView();
            listView.add(PointRecordDtv.convert(list.get(i)));
        }
        result.setDataList(listView);
        result.setTotalCount(pointRecordBiz.getPointRecordCount(param));
        return result.makeSuccessResult();
    }
    */

    /**
     * 积分兑换
     *
     * @param parameter
     * @return
     */
    @Override
    public ResultEx exchangePoint(ExchangeCreditPointParameter parameter) {
        ResultEx resultEx = new ResultEx();
        if (parameter.getSessionIdentity() == null ||
                org.apache.commons.lang3.StringUtils.isBlank(parameter.getSessionIdentity().getUuid()))
            return resultEx.makeFailedResult();

        Integer lockId = NumberUtils.random(0, 1000000);
        Boolean isLockSuccess = false;
        try {
            isLockSuccess = lockService.lock(LockerUtils.UNIQUE_LOCK_Key, LockService.DEFAULT_TRY_LOCK_MILLIS,
                    LockService.DEFAULT_AUTO_UNLOCK_MILLIS, lockId);
            if (!isLockSuccess) {
                return resultEx.makeFailedResult("锁定出错！");
            }
            resultEx = transPointBizService.exchangePoint(parameter);
        } catch (Exception e) {
            resultEx = new ResultEx();
            return resultEx.makeFailedResult();
        } finally {
            if (isLockSuccess) {
                lockService.unlock(LockerUtils.UNIQUE_LOCK_Key, lockId);
            }
        }

        return resultEx;
    }

    /**
     * 计算积分兑换
     *
     * @param parameter
     * @return
     */
    @Override
    public ObjectResultEx<PointExchangeCalculateView> calculateExchangePoint(CalculatePointExchangeParameter parameter) {
        ObjectResultEx<PointExchangeCalculateView> resultEx = new ObjectResultEx();

        if (parameter.getSessionIdentity() == null ||
                org.apache.commons.lang3.StringUtils.isBlank(parameter.getSessionIdentity().getUuid()))
            return resultEx.makeFailedResult();

        Integer lockId = NumberUtils.random(0, 1000000);
        Boolean isLockSuccess = false;
        try {
            isLockSuccess = lockService.lock(LockerUtils.UNIQUE_LOCK_Key, LockService.DEFAULT_TRY_LOCK_MILLIS,
                    LockService.DEFAULT_AUTO_UNLOCK_MILLIS, lockId);
            if (!isLockSuccess) {
                return resultEx.makeFailedResult("锁定出错！");
            }

            resultEx = transPointBizService.calculateExchangePoint(parameter);
        } catch (Exception e) {
            return resultEx.makeFailedResult();
        } finally {
            if (isLockSuccess) {
                lockService.unlock(LockerUtils.UNIQUE_LOCK_Key, lockId);
            }
        }

        return resultEx.makeSuccessResult();
    }
}
